package com.reggie.flavor.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.reggie.dish.domain.Dish;
import com.reggie.uuid.SnowflakeIdUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reggie.flavor.mapper.DishFlavorMapper;
import com.reggie.flavor.domain.DishFlavor;
import com.reggie.flavor.service.IDishFlavorService;

/**
 * 菜品口味关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-27
 */
@Service
public class DishFlavorServiceImpl implements IDishFlavorService 
{
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 查询菜品口味关系
     * 
     * @param id 菜品口味关系主键
     * @return 菜品口味关系
     */
    @Override
    public DishFlavor selectDishFlavorById(Long id)
    {
        return dishFlavorMapper.selectDishFlavorById(id);
    }

    /**
     * 查询菜品口味关系列表
     * 
     * @param dishFlavor 菜品口味关系
     * @return 菜品口味关系
     */
    @Override
    public List<DishFlavor> selectDishFlavorList(DishFlavor dishFlavor)
    {
        return dishFlavorMapper.selectDishFlavorList(dishFlavor);
    }

    /**
     * 新增菜品口味关系
     * 
     * @param flavor 菜品口味关系
     * @return 结果
     */
    @Override
    public int insertDishFlavor(DishFlavor flavor)
    {
        List<String> arr = flavor.getValue();
        //字符串拼接
        StringBuilder builder = new StringBuilder("[");
        for (int j = 0; j < arr.size(); j++) {
            builder.append(j < arr.size() - 1 ? "\"" + arr.get(j) + "\"" + "," : "\"" + arr.get(j) + "\"");
        }
        builder.append("]");
        flavor.setId(new SnowflakeIdUtils(5, 8).nextId());
        flavor.setValueStr(builder.toString());
        flavor.setCreateUser(SecurityUtils.getUserId());
        flavor.setCreateTime(DateUtils.getNowDate());
        return dishFlavorMapper.insertDishFlavor(flavor);
    }

    /**
     * 修改菜品口味关系
     * 
     * @param dishFlavor 菜品口味关系
     * @return 结果
     */
    @Override
    public int updateDishFlavor(DishFlavor dishFlavor)
    {
        dishFlavor.setUpdateTime(DateUtils.getNowDate());
        return dishFlavorMapper.updateDishFlavor(dishFlavor);
    }

    /**
     * 批量删除菜品口味关系
     * 
     * @param ids 需要删除的菜品口味关系主键
     * @return 结果
     */
    @Override
    public int deleteDishFlavorByIds(Long[] ids)
    {
        return dishFlavorMapper.deleteDishFlavorByIds(ids);
    }

    /**
     * 删除菜品口味关系信息
     * 
     * @param id 菜品口味关系主键
     * @return 结果
     */
    @Override
    public int deleteDishFlavorById(Long id)
    {
        return dishFlavorMapper.deleteDishFlavorById(id);
    }

    @Override
    public List<DishFlavor> selectDishFlavorByDishId(Long id) {
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectDishFlavorByDishId(id)
                .stream().map((item) -> {
                    String value = StringUtils.substringBetween(item.getValueStr(), "[", "]");
                    String[] arr = StringUtils.split(value, ",");
                    for (int i = 0; i < arr.length; i++) {
                        arr[i] = StringUtils.substringBetween(arr[i], "\"", "\"");
                    }

                    item.setValue(Arrays.asList(arr));
                    return item;
                }).collect(Collectors.toList());
        return dishFlavors;
    }

    @Override
    public int updateDishFlavors(Dish dish) {
        int i1 = dishFlavorMapper.deleteDishFlavorByDishId(dish.getId());
        System.out.println("删除的返回值："+i1);
        if (null == dish.getDishFlavors() || dish.getDishFlavors().size() == 0) {
            System.out.println("没东西");
            return 1;
        }
        int num = 0;
        for (int i = 0; i < dish.getDishFlavors().size(); i++) {
            System.out.println("dishFlavor:::"+dish.getDishFlavors().get(i));
            dish.getDishFlavors().get(i).setDishId(dish.getId());
            num += this.insertDishFlavor(dish.getDishFlavors().get(i));
        }

        System.out.println("num==" + num);
        return num;
    }


}
